<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="30%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input
            placeholder="输入角色名称"
            style="width: 100%"
            v-model="ruleForm.name"
          />
        </el-form-item>
        <el-form-item label="角色代码" prop="code">
          <el-input
            placeholder="输入角色代码"
            style="width: 100%"
            v-model="ruleForm.code"
          />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input
            placeholder="排序"
            style="width: 100%"
            v-model="ruleForm.sort"
          />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input
            placeholder="描述"
            type="textarea"
            :row="2"
            style="width: 100%"
            v-model="ruleForm.desc"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">有效</el-radio>
            <el-radio :label="0">无效</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addApi, saveApi } from "@/plugins/api/system/role";
import { ElMessage } from "element-plus";
const props = defineProps({
  treeData: {
    type: Array,
    default: [],
  },
});
let ruleForm = ref({});
let rules = {
  code: [{ required: true, message: "请输入角色代码", trigger: "blur" }],
  name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  if (type == "add") {
    title.value = "新增角色";
    ruleForm.value = {};
    ruleForm.value.status = 1;
  } else {
    title.value = "编辑角色";
    ruleForm.value = Object.assign({}, row);
  }

  dialogVisible.value = true;
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.value.id) {
        saveApi(ruleForm.value).then(() => {
          ElMessage.success("编辑成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        addApi(ruleForm.value).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
